package com.hzlx.operations.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.hzlx.operations.mapper.OperationsMapper;
import com.hzlx.operations.domain.Operations;
import com.hzlx.operations.service.IOperationsService;

/**
 * 工序Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-01
 */
@Service
public class OperationsServiceImpl implements IOperationsService 
{
    @Autowired
    private OperationsMapper operationsMapper;
    @Resource
    private RedisTemplate<String, Object> redisTemplateObject;
    String KEY = "base-data-service:operations";
    /**
     * 查询工序
     * 
     * @param operationId 工序主键
     * @return 工序
     */
    @Override
    public Operations selectOperationsByOperationId(Long operationId)
    {
        return operationsMapper.getOperationWithParams(operationId);
    }

    /**
     * 查询工序列表
     * 
     * @param operations 工序
     * @return 工序
     */
    @Override
    public List<Operations> selectOperationsList(Operations operations)
    {
        List<Operations> list = operationsMapper.selectOperationsList(operations);
        redisTemplateObject.opsForValue().set(KEY,list);
        return list;
    }

    @Override
    public Operations selectOperationsWithParams(Long operationId) {
        String key = KEY + ":" + operationId;

        try {
            // 直接从Redis获取，由GenericJackson2JsonRedisSerializer自动反序列化
            Operations operations = (Operations) redisTemplateObject.opsForValue().get(key);

            if (operations != null) {
//                logger.info();
                System.out.println("\n从redis中获取\n："+operations+"\n");
                return operations;
            }
        } catch (Exception e) {
           // logger.error("从Redis获取数据异常，key: {}", key, e);
            System.out.println("\n从Redis获取数据异常:"+ key);
            // 反序列化失败时清除错误缓存
            redisTemplateObject.delete(key);
        }

        // 从数据库查询
        Operations operations = operationsMapper.getOperationWithParams(operationId);

        if (operations != null) {
            try {
                // 直接存储对象，由GenericJackson2JsonRedisSerializer自动序列化
                redisTemplateObject.opsForValue().set(key, operations);

               // logger.info("将查询结果存入redis中：{}", operations);
                System.out.println("\n将查询结果存入redis中：\n"+operations+"\n");
            } catch (Exception e) {
                e.printStackTrace();
               // logger.error("存储数据到Redis异常", e);
                System.out.println("\n存储数据到Redis异常:"+ e);
            }
        }
        return operations;
    }

    /**
     * 新增工序
     * 
     * @param operations 工序
     * @return 结果
     */
    @Override
    public int insertOperations(Operations operations)
    {
        return operationsMapper.insertOperations(operations);
    }

    /**
     * 修改工序
     * 
     * @param operations 工序
     * @return 结果
     */
    @Override
    public int updateOperations(Operations operations)
    {
        return operationsMapper.updateOperations(operations);
    }

    /**
     * 批量删除工序
     * 
     * @param operationIds 需要删除的工序主键
     * @return 结果
     */
    @Override
    public int deleteOperationsByOperationIds(Long[] operationIds)
    {
        return operationsMapper.deleteOperationsByOperationIds(operationIds);
    }

    /**
     * 删除工序信息
     * 
     * @param operationId 工序主键
     * @return 结果
     */
    @Override
    public int deleteOperationsByOperationId(Long operationId)
    {
        return operationsMapper.deleteOperationsByOperationId(operationId);
    }
}
